/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.DataCache;
import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisPatient extends Model<LisPatient> {

    public static final LisPatient lisSqdDao = new LisPatient();
    private String hospital_code_start="XT";

    public LisPatient() {
         if (DataCache.getIntance().getConfig().get("patientIdStartChar") != null) {
            hospital_code_start = DataCache.getIntance().getConfig().get("patientIdStartChar");
        }
    }
    
    public void updateXX(Controller con) throws Exception {
        findById(con.getPara("pk"))
                .set("PATIENTNAME", con.getPara("brxm"))
                .set("SEX", con.getPara("sex"))
                .set("AGE", con.getPara("age"))
                .update();
    }

    public String insertInfo(Controller con) throws Exception {
        String bir = con.getPara("birth");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Timestamp birth = null;
        int age = 0;
        if (bir != null) {
            java.util.Date birt = sdf.parse(bir);
//            birth = new java.sql.Timestamp(birt.getTime());
//            age = DateUtil.getCurrentAgeByBirthdate(bir);
        }
        String id = getPatientId();
        String pk = getPk();
        set("PK", pk)
                .set("PATIENTID", checkStartPatientId(id))
                .set("PATIENTNAME", con.getPara("name"))
                .set("SEX", con.getPara("sex"))
//                .set("BIRTHDAY", birth)
                .set("PHONENO", con.getPara("phone"))
                .set("AGE",  con.getPara("age"))
                .save();
        return pk;
    }

    public void updateInfo(Controller con) throws Exception {
        String bir = con.getPara("birth");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Timestamp birth = null;
        int age = 0;
        if (bir != null) {
            java.util.Date birt = sdf.parse(bir);
            birth = new java.sql.Timestamp(birt.getTime());
            age = DateUtil.getCurrentAgeByBirthdate(bir);
        }
        findById(con.getPara("brpk"))
                .set("PATIENTNAME", con.getPara("name"))
                .set("SEX", con.getPara("sex"))
//                .set("BIRTHDAY", birth)
                .set("AGE", con.getPara("age"))
                .set("PHONENO", con.getPara("phone"))
                .update();
    }

    public String getPk() {
        List<Record> list = Db.use("mysql").find("select * from lis_patient limit 0,10");
        String value = "1";
        if (list.size() > 0) {
            value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(PK) as id from lis_patient").get("id") + "") + 1) + "";
        }
        return value;
    }

    public String getPatientId() {
        // List<Record> list=Db.use("mysql").find("select * from lis_patient limit 0,10");

        String value = "XT";
        if (DataCache.getIntance().getConfig().get("patientIdStartChar") != null) {
            value = DataCache.getIntance().getConfig().get("patientIdStartChar");
        }

        LisPatient re = findFirst("SELECT MAX(PK) as id from lis_patient");
        System.out.println(re);
        if (re == null || re.getInt("id") == null) {

            return value + "000000";

        } else if ((re.getInt("id") + "").length() == 1) {
            value += "00000";
        } else if ((re.getInt("id") + "").length() == 2) {
            value += "0000";
        } else if ((re.getInt("id") + "").length() == 3) {
            value += "000";
        } else if ((re.getInt("id") + "").length() == 4) {
            value += "00";
        } else if ((re.getInt("id") + "").length() == 5) {
            value += "0";
        }
        return value + re.getInt("id") + "";
    }

    public String saveInfo(Map<String, String> map) throws Exception {
        String infoPk = null;
        System.out.println(map.get("PATIENTID"));
        String id = map.get("PATIENTID");
        if (map.get("PATIENTID") == null || map.get("PATIENTID").equalsIgnoreCase("")) {

            id = getPatientId();
        } else {

            infoPk = checkPatientId(map.get("PATIENTID"), map.get("PATIENTNAME"));

        }
        if (infoPk == null) {
            System.out.println(id + "--------------------");
            set("PATIENTID", checkStartPatientId(id))
                    .set("PATIENTNAME", map.get("PATIENTNAME"))
                    .set("SEX", map.get("SEX"))
                    .set("BIRTHDAY", map.get("BIRTHDAY"))
                    .set("PHONENO", map.get("PHONENO"))
                    .set("AGE", map.get("AGE"))
                    .set("DATA1", DateUtil.getDateEN())
                    .save();
            infoPk = findFirst("SELECT MAX(PK) as id from lis_patient").get("id") + "";
        }else{
        updatePatientInfo(map, id);
        }

        return infoPk;
    }

    public String checkPatientId(String patientId, String patientName) {
        try {
            return findFirst("select * from lis_patient where PATIENTID=? AND PATIENTNAME=?", patientId, patientName).get("PK") + "";
        } catch (Exception e) {
            return null;
        }
    }

    public void deleteInfo(Controller con) {
        deleteById(con.getPara("brpk"));
    }

    public void updatePatientInfo(Map<String, String> map, String patientid) {
        Record re = new Record();
                re.set("PATIENTNAME", map.get("PATIENTNAME"))
                .set("SEX", map.get("SEX"))
                .set("BIRTHDAY", map.get("BIRTHDAY"))
                .set("AGE", map.get("AGE"));
                
                Db.update("update lis_patient set PATIENTNAME=?,SEX=?,BIRTHDAY=?,AGE=? WHERE PATIENTID=? ", map.get("PATIENTNAME"),map.get("SEX"),map.get("BIRTHDAY"), map.get("AGE"),patientid);
                

    }
    public String saveOtherInfo(Map<String,String> info){
     //保存病人信息
      
        this.set("PATIENTID", info.get("PATIENTID"));
        this.set("PATIENTNAME", info.get("PATIENTNAME"));
        this.set("SEX", info.get("SEX"));
        this.set("BIRTHDAY", info.get("BIRTHDAY"));
        this.set("AGE", info.get("AGE"));
        this.set("PHONENO", info.get("PHONENO"));
        this.set("CARD_NO", info.get("CARD_NO"));
        this.set("MEDICAL_NO", info.get("MEDICAL_NO"));
        save();
        return get("PK")+"";
    
    }
    public String checkStartPatientId(String patientId){
//        System.out.println(hospital_code_start+patientId+"-------------"+patientId.startsWith(hospital_code_start));
//    if(!patientId.startsWith(hospital_code_start)){
//        System.out.println("2223`&&&&&&&&&&&&&&&&&");
//      return hospital_code_start+patientId;
//    }
    return patientId;
    }
}
